<?php

//namespace app\common\model;
namespace app\admin\model;

use think\Model;

class MettingRoom extends Model {
    // 表名
    protected $name = 'metting_room';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "add_time";
    protected $updateTime = "update_time";

    /**
     * 获取编号最大值
     * @return string
     */
    public function getMaxCode() {
        $codePrefix = 'HS';
        $defaultCode = sprintf('%s%05s',date('Ymd'),'1');
        $maxCode = $this->max('code',0,false);
        if ($maxCode) {
            $maxCode = str_replace($codePrefix,'',$maxCode)+1;
        } else {
            $maxCode = $defaultCode;
        }
        return sprintf('%s%s',$codePrefix,$maxCode);
    }

    public function community(){
        return $this->belongsTo('Community','community_id','code')->setEagerlyType(0);
    }

    public function building(){
        return $this->belongsTo('Building','building_code','code')->setEagerlyType(0);
    }

    /**
     * 根据小区编号，获取房产列表
     * @param $cmCode
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getHouseByCMCode($cmCode) {
        $result = $this->where(array('community_code'=>$cmCode))->field('code,name')->select();
        return $result;
    }
}